package com.dataart.training.crimequalification.dao;

import com.dataart.training.crimequalification.entity.QualificationTaskEntity;
import com.dataart.training.crimequalification.entity.TaskSolutionResultEntity;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class QualificationTaskDAOImpl implements QualificationTaskDAO {

    //-----------------------------------------------------------------

    @Autowired
    private SessionFactory sessionFactory;

    // ----------------------------------------------------------------

    /**
     * @return
     */
    @Override
    public List<QualificationTaskEntity> getAllTasks() {
        return sessionFactory.getCurrentSession()
                .createQuery("from QualificationTaskEntity order by taskId").list();
    }

    /**
     * @param taskId
     * @return
     */
    @Override
    public QualificationTaskEntity getTaskById(Integer taskId) {
        return (QualificationTaskEntity) sessionFactory.getCurrentSession().
                createCriteria(QualificationTaskEntity.class)
                .add(Restrictions.eq("taskId", taskId)).uniqueResult();
    }

    /**
     * @param solutionResult
     */
    @Override
    public void saveUserSubmittedTaskSolutionResult(TaskSolutionResultEntity solutionResult) {
        this.sessionFactory.getCurrentSession().save(solutionResult);
    }

    /**
     * @return
     */
    @Override
    public List<TaskSolutionResultEntity> getAllSolutions() {
        return sessionFactory.getCurrentSession()
                .createQuery("from TaskSolutionResultEntity order by date").list();
    }
}
